//
//  EmailRegisterViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/24.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class EmailRegisterViewController: AccountBaseViewController,InputViewDelegate {
    
    @IBOutlet weak var titleL: UILabel!
    @IBOutlet weak var emailInput: InputView!
    @IBOutlet weak var imageCodeInput: InputView!
    @IBOutlet weak var readedBtn: UIButton!
    @IBOutlet weak var serverDetailBtn: UIButton!
    @IBOutlet weak var nextBtn: SureButton!
    @IBOutlet weak var phoneRegisterBtn: UIButton!
    @IBOutlet weak var loginBtn: UIButton!
    
    lazy var registerViewModel:RegisterOrResetViewModel = RegisterOrResetViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    func initUI() {
        self.titleL.text = kText("邮箱注册")
        self.emailInput.delegate = self
        self.emailInput.placeholder = kText("请输入您的邮箱")
        self.imageCodeInput.delegate = self
        self.imageCodeInput.style = .imageVerify
        self.imageCodeInput.placeholder = kText("请输入图片验证码")
        
//        let text = kText("已阅读并同意《C2C产品服务条款》")
//        let attributedString = NSMutableAttributedString(string:text)
//
//        let range = (text as NSString).range(of: "《")
//        if range.location != NSNotFound {
//            if ThemeManager.default.currentThemeMode() == .day {
//                attributedString.addAttributes([
//                    NSAttributedStringKey.foregroundColor:kTitleColor
//                    ], range: NSRange(location: 0, length: range.location))
//            }else {
//                attributedString.addAttributes([
//                    NSAttributedStringKey.foregroundColor:UIColor(hexStr: "#e7ebf4")
//                    ], range: NSRange(location: 0, length: range.location))
//            }
//            attributedString.addAttributes([
//                NSAttributedStringKey.foregroundColor:kThemeColor
//                ], range: NSRange(location: range.location, length: attributedString.length - range.location))
//        }
//
//
//        self.serverDetailBtn.setAttributedTitle(attributedString, for: .normal)
        
        readedBtn.isSelected = true
        readedBtn.isHidden = true
        self.serverDetailBtn.isHidden = true
        
        self.nextBtn.setTitle(kText("下一步"), for: .normal)
        self.phoneRegisterBtn.setTitle(kText("手机注册"), for: .normal)
        self.loginBtn.setTitle(kText("登录"), for: .normal)
        self.nextBtn.isEnabled = false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        initData()
    }
    
    func initData() {
        registerViewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_imageVerify {
                if isSuccess {
                    self?.imageCodeInput.imageCode = self?.registerViewModel.image
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_emailCode {
                if isSuccess {
                    let vc = SecondRegisterOrResetViewController()
                    vc.type = 1
                    vc.viewModel = self?.registerViewModel
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        registerViewModel.getImageCode()
    }
    // MARK: - event
    @IBAction func readed(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        changeSureBtn()
    }
    
    @IBAction func next(_ sender: SureButton) {
        dPrint("next")
        self.view.endEditing(true)
        self.registerViewModel.sendEmailCode(email: self.emailInput.input.text!, imageCode: self.imageCodeInput.input.text!)
    }
    
    @IBAction func server(_ sender: UIButton) {
        
        dPrint("server")
        
    }
    
    @IBAction func phoneRegister(_ sender: UIButton) {
        self.navigationController?.customReplaceTopPushViewController(MobileRegisterViewController(), animated: true)
    }
    
    @IBAction func login(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    func changeSureBtn() {
        if let emailCount = self.emailInput.input.text?.count,let imageCodeCount = self.imageCodeInput.input.text?.count, emailCount > 0,  imageCodeCount > 0,self.readedBtn.isSelected == true {
            self.nextBtn.isEnabled = true
        }else {
            self.nextBtn.isEnabled = false
        }
    }
    
    func inputViewTextDidChange(inputView: InputView) {
        dPrint("inputViewTextDidChange")
        changeSureBtn()
    }
    func inputViewImageVerifyClick(inputView: InputView) {
        dPrint("inputViewImageVerifyClick")
        self.registerViewModel.getImageCode()
    }
    
}
